/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.alien.admin.modules.system.service.impl;

import com.alien.admin.modules.system.domain.Env;
import com.alien.admin.utils.ValidationUtil;
import com.alien.admin.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.alien.admin.modules.system.repository.EnvRepository;
import com.alien.admin.modules.system.service.EnvService;
import com.alien.admin.modules.system.service.dto.EnvDto;
import com.alien.admin.modules.system.service.dto.EnvQueryCriteria;
import com.alien.admin.modules.system.service.mapstruct.EnvMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import com.alien.admin.utils.PageUtil;
import com.alien.admin.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author zhouseyi
* @date 2021-10-29
**/
@Service
@RequiredArgsConstructor
public class EnvServiceImpl implements EnvService {

    private final EnvRepository envRepository;
    private final EnvMapper envMapper;

    @Override
    public Map<String,Object> queryAll(EnvQueryCriteria criteria, Pageable pageable){
        Page<Env> page = envRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(envMapper::toDto));
    }

    @Override
    public List<EnvDto> queryAll(EnvQueryCriteria criteria){
        return envMapper.toDto(envRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public EnvDto findById(Integer id) {
        Env env = envRepository.findById(id).orElseGet(Env::new);
        ValidationUtil.isNull(env.getId(),"Env","id",id);
        return envMapper.toDto(env);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public EnvDto create(Env resources) {
        return envMapper.toDto(envRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Env resources) {
        Env env = envRepository.findById(resources.getId()).orElseGet(Env::new);
        ValidationUtil.isNull( env.getId(),"Env","id",resources.getId());
        env.copy(resources);
        envRepository.save(env);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            envRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<EnvDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (EnvDto env : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("名称", env.getName());
            map.put("值", env.getValue());
            map.put("说明", env.getDescription());
            map.put("创建人", env.getCreateBy());
            map.put("修改人", env.getUpdatedBy());
            map.put("创建日期", env.getCreateTime());
            map.put("更新时间", env.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
